package com.henghai.zhenxuan.ui.page.signin;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.henghai.zhenxuan.BR;
import com.henghai.zhenxuan.data.api.ConversionProductApi;
import com.henghai.zhenxuan.data.bean.AddressBean;
import com.henghai.zhenxuan.data.bean.SysUserPrizesBean;
import com.henghai.zhenxuan.domain.request.AddressListRequester;
import com.henghai.zhenxuan.domain.request.ConversionProductRequester;
import com.henghai.zhenxuan.domain.request.SysUserPrizesRequester;
import com.henghai.zhenxuan.ui.page.adapter.PrizeListAdapter;
import com.henghai.zhenxuan.ui.view.AddEditingDialogFragment;
import com.henghai.zhenxuan.R;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.kongzue.dialogx.dialogs.MessageDialog;
import com.kongzue.dialogx.dialogs.TipDialog;
import com.kongzue.dialogx.dialogs.WaitDialog;
import com.kunminx.architecture.ui.page.BaseFragment;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.page.StateHolder;
import com.kunminx.architecture.ui.state.State;

import java.util.ArrayList;

public class PrizeFragment extends BaseFragment {

    private PrizeStates mStates;
    private PrizeListAdapter mAdapter;
    private SysUserPrizesRequester mSysUserPrizesRequester;
    private ConversionProductRequester mConversionProductRequester;
    private AddressListRequester mAddressListRequester;
    private AddressBean mAddress;

    @Override
    protected void initViewModel() {
        mStates = getFragmentScopeViewModel(PrizeStates.class);
        mSysUserPrizesRequester = getFragmentScopeViewModel(SysUserPrizesRequester.class);
        mConversionProductRequester = getFragmentScopeViewModel(ConversionProductRequester.class);
        mAddressListRequester = getFragmentScopeViewModel(AddressListRequester.class);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mSysUserPrizesRequester.getResult().observe(this, httpBeanDataResult -> {
            if (!httpBeanDataResult.getResponseStatus().isSuccess()) {
                TipDialog.show(httpBeanDataResult.getResponseStatus().getResponseCode(), WaitDialog.TYPE.ERROR);
                return;
            }
            mStates.prizeList.set(httpBeanDataResult.getResult().getData().getRows());
        });
        mSysUserPrizesRequester.request(this);


        mAddressListRequester.getResult().observe(this, httpBeanDataResult -> {
            if (!httpBeanDataResult.getResponseStatus().isSuccess()) {
                TipDialog.show(httpBeanDataResult.getResponseStatus().getResponseCode(), WaitDialog.TYPE.ERROR);
                return;
            }
            if (httpBeanDataResult.getResult().getData().getTotal() > 0) {
                mAddress = httpBeanDataResult.getResult().getData().getRows().get(0);
            }

        });

        //获取收货地址列表
        mAddressListRequester.request(this);

        //商品兑换结果
        mConversionProductRequester.getResult().observe(this, httpBeanDataResult -> {
            if (!httpBeanDataResult.getResponseStatus().isSuccess()) {
                TipDialog.show(httpBeanDataResult.getResponseStatus().getResponseCode(), WaitDialog.TYPE.ERROR);
                return;
            }
            TipDialog.show(getActivity().getString(com.henghai.zhenxuan.R.string.paymentsuccessful), WaitDialog.TYPE.SUCCESS);

            //重新获取奖品列表
            mSysUserPrizesRequester.request(this);
        });

    }

    public class ListenerHandler implements OnTitleBarListener {
        @Override
        public void onLeftClick(TitleBar titleBar) {
            OnTitleBarListener.super.onLeftClick(titleBar);
            nav().navigateUp();
        }

        @Override
        public void onRightClick(TitleBar titleBar) {
            OnTitleBarListener.super.onRightClick(titleBar);
        }
    }

    private void convert(SysUserPrizesBean.RowsBean prizeBean) {
        ArrayList<ConversionProductApi.ProductInfoBean> productInfoList = new ArrayList<>();
        ConversionProductApi.ProductInfoBean productInfoBean = new ConversionProductApi.ProductInfoBean();
        productInfoBean.setId(prizeBean.getProductId() + "");
        productInfoBean.setProductQuantity("1");
        productInfoList.add(productInfoBean);
        mConversionProductRequester.request(getViewLifecycleOwner(), mAddress == null ? "" : mAddress.getId() + "", productInfoList, prizeBean.getId() + "");
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        mAdapter = new PrizeListAdapter(getContext());
        mAdapter.setOnItemBtClickListener((prizeBean, position) -> {
            if (prizeBean.getProductCategory() != null && prizeBean.getProductCategory().equals("2")) {
                MessageDialog.show("提示", "是否兑换").setOkButton("确认").setOkButtonClickListener((messageDialog, view) -> {
                    convert(prizeBean);
                    return false;
                }).setCancelButton("取消");
                return;
            }
            AddEditingDialogFragment.newInstance(mAddress).setOnItemBtClickListener(new AddEditingDialogFragment.OnItemBtClickListener() {
                @Override
                public void onEditClick() {
                    mAddressListRequester.request(getViewLifecycleOwner());
                }

                @Override
                public void onSaveClick(AddressBean addressBean) {
                    if (mAddress == null) {
                        mAddress = addressBean;
                    }
                    //获取收货地址列表
                    mAddressListRequester.request(getViewLifecycleOwner());

                    if (mAddress == null ||
                            mAddress.getConsigneeAddress() == null ||
                            mAddress.getConsigneeAddress().isEmpty() ||
                            mAddress.getConsigneeName() == null ||
                            mAddress.getConsigneeName().isEmpty() ||
                            mAddress.getConsigneePhone() == null ||
                            mAddress.getConsigneePhone().isEmpty()) {
                        TipDialog.show("请完善收货地址信息");
                        return;
                    }
                    convert(prizeBean);
                }


            }).show(getChildFragmentManager(), AddEditingDialogFragment.class.getSimpleName());
        });
        return new DataBindingConfig(R.layout.fragment_prize, BR.vm, mStates)
                .addBindingParam(BR.adapter, mAdapter).addBindingParam(BR.listener, new ListenerHandler());
    }


    public static class PrizeStates extends StateHolder {
        public final State<ArrayList<SysUserPrizesBean.RowsBean>> prizeList = new State<>(new ArrayList<>());

    }
}
